<html>
<head><meta charset="utf-8"><title>staged compilation · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html">staged compilation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="148190936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148190936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148190936">(Nov 22 2018 at 19:01)</a>:</h4>
<p>So I find myself occasionally wanting staged compilation -- does anyone know of a system that provides that for Rust?</p>



<a name="148196211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148196211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148196211">(Nov 22 2018 at 21:42)</a>:</h4>
<p>Can you describe what that is in broad strokes?</p>



<a name="148196751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148196751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148196751">(Nov 22 2018 at 22:01)</a>:</h4>
<p>Sorry, I should have filled in. Staged compilation, aka <a href="https://en.wikipedia.org/wiki/Multi-stage_programming" target="_blank" title="https://en.wikipedia.org/wiki/Multi-stage_programming">https://en.wikipedia.org/wiki/Multi-stage_programming</a>, is a technique where some part of the code is itself generated by code. In this case, I'm actually hoping for the "partial evaluation" aspect of it, where you can have expensive computations with relatively compact outputs be computed at compile-time and then inserted into the final executable. May have overlaps with <a class="stream" data-stream-id="146212" href="/#narrow/stream/146212-t-compiler.2Fconst-eval">#t-compiler/const-eval</a>.</p>



<a name="148212686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148212686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148212686">(Nov 23 2018 at 07:07)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <span class="emoji emoji-1f44b" title="wave">:wave:</span></p>



<a name="148219057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148219057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148219057">(Nov 23 2018 at 09:50)</a>:</h4>
<p><span class="user-mention" data-user-id="137157">@JP Sugarbroad</span> There's an ongoing effort to making const eval more powerful. What kind of things do you have in mind?</p>



<a name="148219297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148219297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148219297">(Nov 23 2018 at 09:56)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> I don't think const eval, with the current approach, can ever be as good as staged compilation. See <a href="https://anydsl.github.io/Impala.html" target="_blank" title="https://anydsl.github.io/Impala.html">https://anydsl.github.io/Impala.html</a> for a nice example of what staged compilation can do (even with a Rust-inspired syntax^^)</p>



<a name="148228696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148228696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148228696">(Nov 23 2018 at 13:19)</a>:</h4>
<p>oh... I misunderstood what was desired</p>



<a name="148228748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148228748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148228748">(Nov 23 2018 at 13:20)</a>:</h4>
<p>staged compilation seems trivial as a MirPass</p>



<a name="148228773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148228773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148228773">(Nov 23 2018 at 13:20)</a>:</h4>
<p>it's essentially just const propagation</p>



<a name="148239738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148239738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148239738">(Nov 23 2018 at 16:53)</a>:</h4>
<p>I thought const stuff couldn't allocate?</p>



<a name="148239864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148239864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148239864">(Nov 23 2018 at 16:56)</a>:</h4>
<p>just like the compiler cannot do partial evaluation, it cannot allocate inside constants. That's not an inherent show stopper though. <a href="https://github.com/solson/miri" target="_blank" title="https://github.com/solson/miri">https://github.com/solson/miri</a> can do heap allocations just fine</p>



<a name="148239911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148239911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148239911">(Nov 23 2018 at 16:57)</a>:</h4>
<p>once we have figured out <a href="https://github.com/rust-rfcs/const-eval/pull/8" target="_blank" title="https://github.com/rust-rfcs/const-eval/pull/8">https://github.com/rust-rfcs/const-eval/pull/8</a> (which will allow writing <code>const</code> <code>Drop</code> impls), we can think about allocations in constants</p>



<a name="148239919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148239919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148239919">(Nov 23 2018 at 16:57)</a>:</h4>
<p>we could technically do so even before that, but it seems needlessly complex to do so</p>



<a name="148244952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148244952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148244952">(Nov 23 2018 at 19:05)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> Nice! Okay, I'll keep an eye out for that. Full staged compilation does require the ability to execute pretty much arbitrary code, though, and then reconstruct the resulting value in the final object.</p>



<a name="148245038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148245038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148245038">(Nov 23 2018 at 19:06)</a>:</h4>
<p>The other thing to note is that staged compilation usually doesn't require tagging like const eval does. Is there any plan to infer const-ness?</p>



<a name="148251167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148251167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148251167">(Nov 23 2018 at 21:58)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> I have no idea how you think staged compilation can be trivial^^</p>



<a name="148251170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148251170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148251170">(Nov 23 2018 at 21:58)</a>:</h4>
<p>its const propagation on steroids</p>



<a name="148251178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148251178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148251178">(Nov 23 2018 at 21:59)</a>:</h4>
<p>needs symbolic miri <em>at the least</em></p>



<a name="148251241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148251241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148251241">(Nov 23 2018 at 22:00)</a>:</h4>
<p>or rather, something symbolic.</p>



<a name="148251248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148251248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148251248">(Nov 23 2018 at 22:00)</a>:</h4>
<p>it doesnt need tons of other stuff miri does</p>



<a name="148272354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148272354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148272354">(Nov 24 2018 at 10:38)</a>:</h4>
<p>I don't understand... why can't you just copy the MIR of a function, remove an argument, and initialize the corresponding variable with the constant?</p>



<a name="148272697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148272697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148272697">(Nov 24 2018 at 10:50)</a>:</h4>
<p>staged compilation is about <em>guaranteed</em> compile-time unrolling</p>



<a name="148272698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148272698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148272698">(Nov 24 2018 at 10:50)</a>:</h4>
<p>like, if you have a staged loop, if it is annotated as such, it should be guaranteed to be unrolled</p>



<a name="148273222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/staged%20compilation/near/148273222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/staged.20compilation.html#148273222">(Nov 24 2018 at 11:06)</a>:</h4>
<p>oh, guarantees, well yea :D that's a different story</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>